<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\api\model\RecommendContent as RecommendModel;
use app\store\model\RecommendImage as RecommendImageModel;
use app\store\model\RecommendGoods as RecommendGoodsModel;
use app\api\service\Collect as CollectService;
use app\api\model\{Goods as GoodsModel};

/**
 * 推荐商品文章控制器
 * Class Article
 * @package app\api\controller
 */
class Recommend extends Controller
{

     /**
     * 推荐商品文章列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function list(): \think\response\Json
    {
        // 获取列表数据
        $model = new RecommendModel;
        //var_dump($this->request->param());exit;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }


    /**
     * 推荐商品详情
     * @param int $recommendId
     * @return \think\response\Json
     * @throws \cores\exception\BaseException
     */
    public function detail(int $recommendId)
    {
        $php_token = $this->request->header('Access-Token');
        // var_dump($php_token);
        $detail = RecommendModel::getDetail($recommendId);
        // var_dump($detail);
        $RecommendImageModel = new RecommendImageModel();
        $RecommendGoodsModel = new RecommendGoodsModel();
        // 遍历商品列表整理数据
        $detail['carousel_images'] =  $RecommendImageModel->getDataList((int)$detail['recommend_id']);
        $detail['carousel_images'] =  array_column($detail['carousel_images'],'preview_url');

        $detail['goods_ids'] =  $RecommendGoodsModel->getRecommendGoodsIds((int)$detail['recommend_id']);
        // 获取列表数据
        $model = new GoodsModel;
        $CollectService = new CollectService;
        //var_dump($this->request->param());exit;
        $goodslist = $model->getList(['goodsIds'=>$detail['goods_ids'],'limit'=>'100','sortType'=>'recommend']);
        $goodslist =  $goodslist->toArray();
      
        foreach($goodslist['data'] as $k=>$v){
            $collectStatus = $CollectService->verifyCollect($v['goods_id'],$php_token);
            $goodslist['data'][$k]['collectStatus'] = $collectStatus;
            $goodslist['data'][$k]['goods_images'] = array_column($v['goods_images'],"preview_url") ; 
        }
        $detail['goods_detail_list'] = $goodslist['data'];
        // $detail['describe'] =   strip_tags(htmlspecialchars_decode($detail['describe']));       
        $detail['describe'] =   htmlspecialchars_decode($detail['describe']);        

        // var_dump($detail);
        // 获取image (这里不能用with因为编辑页需要image对象)
        !empty($detail) && $detail['image'];
        return $this->renderSuccess(compact('detail'));
    }
}
